<template>
    <div ref="myHoverableElement" :style="{
        backgroundColor: isHovered || isActive ? '#3b82f6' : 'transparent',
        color: isHovered || isActive ? '#fff' : 'black'
    }" class="itembox">
        <div class="avator">
            <img src="../assets/avator.jpg" style="width: 100%;height: 100%;" alt="">
        </div>
        <div class="contentbox">
            <div class="topcontent">
                <div class="name">
                    {{ item.userName }}</div>
                <div style="display: flex;align-items: center;">
                    <div :style="{
                        color: isHovered || isActive ? '#fff' : '#a0a0a0'
                    }" class="time">
                        {{ formatWeChatTime(item.loginTime) }} </div>
                    <div style="width: 8px;height: 8px;border-radius: 50%;" :style="{
                        background: item.isOnline ? '#1778ff' : 'gray',
                    }"></div>
                </div>
            </div>
            <div :style="{
                color: isHovered || isActive ? '#fff' : '#a0a0a0'
            }" class="msg">{{ lastMsg(item.lastMsg) }}</div>
        </div>
    </div>
</template>

<script setup lang="ts">
import storage from '@/utils/storage'
import { useElementHover } from '@vueuse/core'
import { computed, useTemplateRef } from 'vue'
import { formatWeChatTime } from '@/utils/timeshow'
defineProps({
    item: {
        type: Object,
        default: () => ({})
    },
    isActive: {
        type: Boolean,
        default: false
    }
})

const lastMsg = computed(() => (val: any) => {
    if (!val) {
        return "-";
    } else {
        let res = val.filter((es: any) => {
            return (
                (es.fid == es.fid && es.tid == storage.get('userinfo')._id) ||
                (es.fid == storage.get('userinfo')._id && es.tid == es.tid)
            );
        });

        if (res && res.length > 0) {
            let sitem = res.sort((a: any, b: any) => a.time - b.time)[res.length - 1];

            switch (sitem.type) {
                case "text":
                    return sitem.lastword;

                case "image":
                    return `[图片]`;

                case "video":
                    return "[视频]";

                case "word":
                    return "[DOC文件]";

                case "exe":
                    return "[EXE文件]";

                case "pdf":
                    return "[PDF文件]";

                case "textFile":
                    return "[文本文件]";

                case "excel":
                    return "[EXCEL文件]";

                case "zip":
                    return "[压缩文件]";

                default:
                    return "[未知文件]";
            }
        }
    }
});
const myHoverableElement = useTemplateRef<HTMLButtonElement>('myHoverableElement')
const isHovered = useElementHover(myHoverableElement)
</script>

<style lang="scss" scoped>
.itembox {
    width: 100%;
    height: 70px;
    padding: 10px;
    box-sizing: border-box;
    display: flex;
    align-items: center;
    overflow: hidden;
    border-radius: 0 0 0 0;
    cursor: pointer;
    border-bottom: 1px solid #e5e5e5;

    .avator {
        width: 50px;
        height: 50px;
        border-radius: 4px;
        flex-shrink: 0;
    }

    .contentbox {
        width: calc(100% - 50px);
        height: 50px;
        margin-left: 14px;
        display: flex;
        flex-direction: column;
        justify-content: space-between;

        .msg {
            color: #a0a0a0;
            font-size: 14px;
        }

        .topcontent {
            display: flex;
            align-items: center;
            width: 100%;
            justify-content: space-between;

            .name {
                display: flex;
                align-items: center;
                max-width: 220px;
                max-height: 1.5em;
                /* 最大高度，等于一行的高度 */
                overflow: hidden;
                /* 隐藏溢出的内容 */
                display: -webkit-box;
                /* 使用弹性盒模型 */
                line-clamp: 1;
                -webkit-line-clamp: 1;
                /* 限制行数为1 */
                -webkit-box-orient: vertical;
                overflow-y: hidden;
                /* 垂直排列 */
                text-overflow: ellipsis;
            }

            .time {
                width: 60px;
                font-size: 12px;
                color: gray;
                display: flex;
                align-items: center;
                justify-content: center;
            }
        }
    }
}
</style>